<#include "/includes/vars.inc"> 
<@pp.dropOutputFile />
<#if includeSettings>
    <#assign entityName = config.settings.@name[0]!"Settings">
    <@pp.changeOutputFile name="${javaDir}${packageDir}/web/support/${entityName?cap_first}ControllerBase.java" />
   
package ${project.@rootPackage}.web.support;

import ${project.@rootPackage}.service.support.${entityName}Service;
import ${project.@rootPackage}.web.util.JsfUtil;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller class for entity ${entityName}
 * <#if project.@author[0]??>@author ${project.@author}</#if>
 */
public abstract class ${entityName}ControllerBase {   

	protected static final Logger logger = Logger.getLogger("${entityName}Controller");
        
    @Autowired
    private PreferenciasService service;
    protected Properties settings;
    <#list config.settings.setting as setting>
    protected ${setting.@type} ${setting.@name};
    </#list>
    
   
    /**
     * Resetea las preferencias a los valores por defecto.
     */
    public void restoreDefaults() {
        if (service.restoreDefaultValues()) {
            settings=null;
            JsfUtil.addSuccessMessage("Preferencias por defecto restauradas");
        } else {
            JsfUtil.addErrorMessage("Ha ocurrido un error al guardar las preferencias por defecto");
        }
    }

    /**
     * Guarda las preferencias actuales.
     */
    public void saveSettings() {
    	<#list config.settings.setting as setting>
    	settings.setProperty("${setting.@name}",${setting.@name}.toString());
    	</#list>
        if (service.saveSettings(settings)) {
            JsfUtil.addSuccessMessage("Preferencias guardadas");
        } else {
            JsfUtil.addErrorMessage("Ha ocurrido un error al guardar las preferencias");
        }
    }

    public Properties getSettings() {
        if (settings != null){
            return settings;
        }
        return service.getSettings();
    }
 
    
    /*
    * GETTERS - SETTERS
    */
    <#list config.settings.setting as setting>
    public ${setting.@type} get${setting.@name?cap_first}() {
    	if (settings == null){
            settings=getSettings();
        }
        <#if setting.@type="String">
        return String.valueOf(settings.getProperty("${setting.@name}"));
        <#elseif setting.@type="Double">
        return Double.valueOf(settings.getProperty("${setting.@name}"));
        <#elseif setting.@type="Integer">
        return Integer.valueOf(settings.getProperty("${setting.@name}"));
        <#elseif setting.@type="Float">
        return Float.valueOf(settings.getProperty("${setting.@name}"));
        <#elseif setting.@type="Character">
        return String.valueOf(settings.getProperty("${setting.@name}"));
        <#else>
        //type not supported, redefine in custom controller
        return String.valueOf(settings.getProperty("${setting.@name}"));
        </#if>
    }

    public void set${setting.@name?cap_first}(${setting.@type} ${setting.@name}) {
        this.${setting.@name} = ${setting.@name};
    }
    </#list>
   

    /*
    public String getStringValue(String key) {
        String res="";
        try {
            res = settings.getProperty(key);
        } catch (Exception e) {
            System.out.println(e);
        }
        return res;
    }

    public Double getDoubleValue(String key) {
        Double number=0d;
        try {
            number = Double.valueOf(settings.getProperty(key));
        } catch (Exception e) {
            System.out.println(e);
        }
        return number;
    }
    
    public Float getFloatValue(String key) {
        Float number=0f;
        try {
            number = Float.valueOf(settings.getProperty(key));
        } catch (Exception e) {
            System.out.println(e);
        }
        return number;
    }
    
    public Integer getIntegerValue(String key) {
        Integer number=0;
        try {
            number = Integer.valueOf(settings.getProperty(key));
        } catch (Exception e) {
           System.out.println(e);
        }
        return number;
    }
    
    public Boolean getBooleanValue(String key) {
        Boolean bool=false;
        try {
            bool = Boolean.valueOf(settings.getProperty(key));
        } catch (Exception e) {
            System.out.println(e);
        }
        return bool;
    }*/
}
</#if>
